#lang racket

(define (make-rat n d)
  (let ((common-divider (gcd n d)))
    (cons (/(* (abs n) (sign (* n d))) common-divider) 
          (/ (abs d) common-divider))))

(define (numer x) (car x))

(define (denom x) (cdr x))

(define (print-rat x)
  (newline)
  (display (numer x))
  (display "/")
  (display (denom x)))

(define (add-rat x y)
  (make-rat (+ (* (numer x) (denom y))
               (* (numer y) (denom x)))
            (* (denom x) (denom y))))

(define (sub-rat x y)
  (make-rat (- (* (numer x) (denom y))
               (* (numer y) (denom x)))
            (* (denom x) (denom y))))

(define (mul-rat x y)
  (make-rat (* (numer x) (numer y))
            (* (denom x) (denom y))))

(define (div-rat x y)
  (make-rat (* (numer x) (denom y))
            (* (denom x) (numer y))))

(define (equal-rat? x y)
  (= (* (numer x) (denom y))
     (* (numer y) (denom x))))

(define (sign x)
  (cond ((or (> x 0) (= x 0)) 1)
        (else -1)))

(define a (make-rat 1 -2))

(define b (make-rat 3 4))

(add-rat a b)

(sub-rat a b)

(mul-rat a b)

(div-rat a b)